<template>
	<view class="container">
		<!-- <view class="video-title-close">
				<view class="video-list-name">
					<view class="video-title-icon"></view>
					<text class="video-title">总的课程名称</text>
				</view>
				<uni-icons type="close" size="22" color="#707070" @click="videoclose"></uni-icons>
			</view> -->
		<checkbox-group @change="payMoney">
			<view class="video-lsits">
				<template v-for="(item, i) in cartList">
					<!-- @tap="payMoney(item.id, item.price, i)" -->
					<view v-if="item.price != 0" class="video-list-items" :key="i">
						<view class="left-box">
							<label>
								<checkbox :value="item.id" color="#B91C27" :checked="item.isClick" @tap="checkItemHandle(i)" />
							</label>
							<view class="video-list-items-img">
								<image :src="item.cover_url" style=""
									mode="aspectFill">
								</image>
								<!-- <view class="video-list-items-img-time">
									<text style="color: #FFFFFF;font-size: 20rpx;">{{item.video_time | filterVideoTime}}</text>
								</view> -->
							</view>
						</view>
						<view class="video-list-items-desc">
							<text style="color: #1A1A1A;font-weight: bold;">{{item.title || ''}}</text>
							<text style="color: #4D4D4D;">{{item.describe || ''}}</text>
							<view class="video-list-items-desc-price">
								<text style="color: #B91C27;">￥{{item.price}}</text>
								<!-- <uni-icons type="cart-filled" size="20" color="#B91C27"></uni-icons> -->
							</view>
						</view>
					</view>
				</template>
			</view>
		</checkbox-group>
		
		<!-- 结算按钮 -->
		<view class="price-box" v-if="payment.shop_id">
			<view class="left-radio">
				<label class="radio">
					<checkbox :checked="isAllChecked" value="1" @tap="radioChange" color="#B91C27" />
					<text class="title">全选</text>
				</label>
			</view>
			<view class="amount-box">
				<text class="amount">合计：</text>
				<text class="price">{{allMoney}}</text>
			</view>
			<view class="confirm-btn" @tap="payMoneyprice">
				立即结算
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapGetters,
		mapMutations
	} from 'vuex'
	export default {
		data() {
			return {
				isAllChecked: false,
				// allMoney: 0,
				apiList: [], // 选中的视频id数组
				payMentQuery: {
					group: '',
					offset: 1,
					limit: 10
				},
				payment: {
					shop_id: ''
				},
			}
		},
		computed: {
			...mapGetters(['cartList']),
			allMoney() {
				let sum = 0
				this.cartList.forEach(key => {
					this.apiList.forEach(item => {
						if (key.id == item) {
							sum += +key.price
						}
					})
				})
				return sum.toFixed(2)
			}
		},
		watch: {
			apiList: {
				handler(value) {
					this.isAllChecked = value.length == this.cartList.length ? true : false
					this.payment.shop_id = value.join(',')
				},
				deep: true
			}
		},
		methods: {
			...mapMutations('cart', ['updateCartList', 'removeCartList']),
			checkItemHandle(i) {
				const query = {
					...this.cartList[i],
					isClick: !this.cartList[i].isClick
				}
				this.cartList.splice(i, 1, query)
			},
			// 支付
			payMoney(e) {
				this.apiList = e.detail.value
			},
			// 立即结算 提交订单
			async payMoneyprice() {
				const res = await this.$http.request('/api/v1/order', this.payment)
				if (res.code == 200) {
					const ress = await this.$http.request('/api/v1/pay', {
						data: JSON.stringify(res.data)
					})
					if (ress.code == 200) {
						// 仅作为示例，非真实参数信息。
						// console.warn(Date.parse(new Date()) + '')
						uni.requestPayment({
							provider: 'wxpay',
							timeStamp: ress.data.config.timestamp,
							nonceStr: ress.data.config.nonceStr,
							package: ress.data.config.package,
							signType: ress.data.config.signType,
							paySign: ress.data.config.paySign,
							success: res => {
								uni.setStorageSync('shopId', JSON.stringify(this.apiList))
								this.removeCartList(this.apiList)
								// this.payment.shop_id = ''
								this.apiList = []
							},
							fail(err) {}
						});
					}
				}
			},
			radioChange(e) {
				console.log(e)
				this.isAllChecked = !this.isAllChecked
				this.cartList.forEach(item => item.isClick = this.isAllChecked)
				this.apiList = this.isAllChecked ? this.cartList.map(item => item.id) : []
				// this.allMoney = 0
				// this.apiList = []
				
				// if (!this.isAllChecked) {
				// 	this.payMentList.forEach((item, index) => {
				// 		item.isClick = false
				// 	})
				// 	this.allMoney = 0
				// } else {
				// 	this.payMentList.forEach((item, index) => {
				// 		item.isClick = true
				// 		this.apiList.push(item.id)
				// 		this.payment.shop_id = this.apiList.join(',')
				// 		this.allMoney += +(item.price)
				// 	})
				// 	this.allMoney = this.allMoney.toFixed(2)
				// }
			
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		padding: 30rpx 30rpx 200rpx;
		background-color: rgb(239, 238, 246);
		width: 100%;
		min-height: 100vh;
	}

	.price-box {
		@include flex-between(100%, 120rpx);
		position: fixed;
		bottom: 0;
		left: 0;
		background-color: #FFFFFF;
		box-shadow: 5rpx 0 10rpx rgba($color: #000000, $alpha: .3);
		padding: 0 20rpx 0 46rpx;
		z-index: 9999;

		.left-radio {
			.title {
				margin-left: 16rpx;
				font-size: 28rpx;
				color: #4D4D4D;
			}
		}

		.amount-box {
			display: flex;
			align-items: center;

			.amount {
				font-size: 28rpx;
				color: #4D4D4D;
				margin-right: 8rpx;
			}

			.price {
				font-size: 24rpx;
				color: #B91C27;
			}
		}

		.confirm-btn {
			@include flex-center(318rpx, 88rpx);
			background: #B91C27;
			border-radius: 44rpx;
			font-size: 36rpx;
			color: #FFFEFE;
		}

	}

	.video-title-close {
		box-sizing: border-box;
		padding-top: 10rpx;
		margin-bottom: 40rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.video-list-name {
		display: flex;
		align-items: center;
	}

	.video-title {
		font-weight: bold;
		color: #4D4D4D;
		letter-spacing: 2rpx;
		box-sizing: border-box;
	}

	.video-title-icon {
		width: 12rpx;
		height: 40rpx;
		border-radius: 20rpx;
		margin-right: 20rpx;
		background-color: #B91C27;
	}

	/* 课程列表 */
	.video-lsits {
		position: relative;
		.video-list-items {
			display: flex;
			// justify-content: space-between;
			margin-bottom: 40rpx;
			border-radius: 20rpx;
			background-color: #fff;
			padding: 30rpx;
			.left-box {
				display: flex;
				align-items: center;
				.video-list-items-img {
					position: relative;
					width: 260rpx;
					height: 210rpx;
					border-radius: 20rpx;
					margin-left: 24rpx;
					image {
						width: 100%;
						height: 100%;
						border-radius: 20rpx;
					}
					.video {
						width: 100%;
						height: 100%;
					}
				}
			}
			.video-list-items-desc {
				width: 100%;
				display: flex;
				flex-direction: column;
				justify-content: space-around;
				margin-left: 40rpx;
				height: 210rpx;
				& > text {
					width: 280rpx;
					@include over-ellip();
				}
			}
		}
	}
	.active-bg {
		box-shadow: 0 5rpx 15rpx rgba($color: #000000, $alpha: .3);
	}
	.video-list-items-img-time {
		background-color: rgba(0, 0, 0, 0.4);
		width: 120rpx;
		height: 50rpx;
		text-align: center;
		display: flex;
		align-items: center;
		justify-content: center;
		position: absolute;
		top: 0;
		left: 0;
		border-radius: 20rpx 0 50rpx 0;
		z-index: 9999;
	}

	

	.video-list-items-desc-price {
		display: flex;
		justify-content: space-between;
	}
</style>
